﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace VisionCloud.Utility.Security
{
    /// <summary>
    /// 跨平台一致性支持(联调使用)
    /// </summary>
    public static class AesCoreHelper
    {
        public static string RgbIV { get; set; } = "ryojvlzmdalyglrj";
        public static string Sceret { get; set; } = "hcxilkqbbhczfeultgbskdmaunivmfuo"; //BZLILKQBBHCZFEULTGBSKDMAUNIVMFUO

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="sceret">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
        public static string Encrypt(string encryptString, string sceret)
        {
            try
            {
                byte[] rgbKey = Encoding.ASCII.GetBytes(sceret);
                byte[] rgbIV = Encoding.ASCII.GetBytes(AesCoreHelper.RgbIV);
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                using (var rijn = Rijndael.Create())
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, rijn.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            csEncrypt.Write(inputByteArray, 0, inputByteArray.Length);
                            csEncrypt.FlushFinalBlock();
                            var encryptedContent = msEncrypt.ToArray();
                            return Convert.ToBase64String(encryptedContent);
                        }
                    }
                }
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="sceret">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string Decrypt(string decryptString, string sceret)
        {
            try
            {
                byte[] rgbKey = Encoding.ASCII.GetBytes(sceret);
                byte[] rgbIV = Encoding.ASCII.GetBytes(AesCoreHelper.RgbIV);
                byte[] inputByteArray = Convert.FromBase64String(decryptString);

                using (var rijn = Rijndael.Create())
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, rijn.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                        {
                            csEncrypt.Write(inputByteArray, 0, inputByteArray.Length);
                            csEncrypt.FlushFinalBlock();
                            var decryptedContent = msEncrypt.ToArray();
                            return Encoding.UTF8.GetString(decryptedContent);
                        }
                    }
                }
            }
            catch
            {
                return decryptString;
            }
        }
    }
}